<template>
  <div class="panel">
    <div ref="chartRef" class="chart"></div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import * as echarts from 'echarts'

const chartRef = ref<HTMLElement | null>(null)
let chart: echarts.ECharts | null = null

const initChart = () => {
  if (!chartRef.value) return
  
  chart = echarts.init(chartRef.value)
  const option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      data: ['当前状态', '理想状态'],
      textStyle: {
        color: '#33ffd0'
      },
      right: '5%',
      bottom: '5%',
      orient: 'vertical',
      itemGap: 20
    },
    radar: {
      indicator: [
        { name: '氮含量', max: 100 },
        { name: '磷含量', max: 100 },
        { name: '钾含量', max: 100 },
        { name: '水分', max: 100 },
        { name: '有机质', max: 100 }
      ],
      splitArea: {
        areaStyle: {
          color: ['rgba(51, 255, 208, 0.1)']
        }
      },
      axisName: {
        color: '#33ffd0'
      },
      splitLine: {
        lineStyle: {
          color: 'rgba(51, 255, 208, 0.2)'
        }
      },
      axisLine: {
        lineStyle: {
          color: 'rgba(51, 255, 208, 0.2)'
        }
      },
      center: ['50%', '55%'],
    },
    series: [
      {
        type: 'radar',
        data: [
          {
            value: [75, 65, 80, 70, 60],
            name: '当前状态',
            areaStyle: {
              color: 'rgba(51, 255, 208, 0.3)'
            },
            lineStyle: {
              color: '#33ffd0',
              width: 2
            },
            itemStyle: {
              color: '#33ffd0'
            }
          },
          {
            value: [85, 75, 90, 80, 70],
            name: '理想状态',
            areaStyle: {
              color: 'rgba(245, 108, 108, 0.3)'
            },
            lineStyle: {
              color: '#F56C6C',
              width: 2,
              type: 'dashed'
            },
            itemStyle: {
              color: '#F56C6C'
            }
          }
        ]
      }
    ]
  }
  
  chart.setOption(option)
}

// 监听窗口大小变化
const handleResize = () => {
  chart?.resize()
}

onMounted(() => {
  initChart()
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  chart?.dispose()
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
.panel {
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chart {
  width: 100%;
  height: 100%;
}
</style> 